import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/order/order_goods_item.dart';
import 'package:cbec_app/pages/my/controller/my_group_buy.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_typedefs/rx_typedefs.dart';

import '../popup/after_sales_popup.dart';

class GroupOrderItem extends StatelessWidget {
  //订单ID
  final String id;
  //订单号
  final String orderSn;
  //订单时间
  final String orderTime;
  //订单金额
  final double totalAmount;
  //订单状态 1、拼团中 2、拼团成功
  final int status;
  //订单商品列表
  final List goodsList;

  //还差几人
  final int? needPeople;

  //拼团 ID
  final String bookId;

  GroupOrderItem({
    super.key,
    required this.orderSn,
    required this.orderTime,
    required this.totalAmount,
    required this.status,
    required this.goodsList,
    required this.id,
    this.needPeople = 0,
    required this.bookId,
  });

  late Widget actionButtons; //操作按钮组
  late Widget statusWidget; //右上角的状态

  final MyGroupBuyController controller = Get.put(MyGroupBuyController());

  @override
  Widget build(BuildContext context) {
    //渲染构建
    switch (status) {
      case 0:
        //拼团中
        actionButtons = Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            _buttons("邀请好友", color: "#F83F2D", () {
              Get.toNamed(Routes.invitedFriends, arguments: bookId);
            }),
          ],
        );

        statusWidget = Text("拼团中,差$needPeople人", style: TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Tools.fromHex("#F83F2D")));

        break;
      case 1:
        //拼团成功
        actionButtons = Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            _buttons("申请售后", color: "#333333", () {
              afterSalesPopup(context, goodsList, id, afterSaleStyle: 1);
            }),
          ],
        );
        statusWidget = Text("拼团成功", style: TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Tools.fromHex("#F83F2D")));
        break;

      case 2:
        //拼团失败
        actionButtons = const Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [],
        );
        statusWidget = Text("拼团失败", style: TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Tools.fromHex("#F83F2D")));
        break;

      default:
    }

    return BaseInteractive(
      onClick: () {
        Get.toNamed(Routes.groupBuyingItemDetailPage, arguments: id);
      },
      childWidget: Container(
        margin: EdgeInsets.only(bottom: 10.w),
        decoration: border_radis(Colors.white),
        // height: 200,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            //标题栏
            Container(
              padding: EdgeInsets.all(10.w),
              // height: 28,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "订单编号：${orderSn}",
                    style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w300),
                  ),
                  statusWidget
                ],
              ),
            ),
            //订单的商品列表
            Container(
              padding: EdgeInsets.only(left: 10.w, right: 10.w),
              child: Column(
                children: [
                  //这里面是商品
                  for (var goodsItem in goodsList) OrderGoodsItem(goodsImg: goodsItem['img']!, title: goodsItem['title']!, tags: goodsItem['tags'] ?? [], price: goodsItem['price']!, num: goodsItem['num'], totalAmount: totalAmount),
                  //这里是实付款

                  //这里是按钮
                  Container(margin: EdgeInsets.only(top: 10.w, bottom: 10.w), child: actionButtons)
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /**
   * 按钮
   */
  Widget _buttons(String title, Callback onPressed, {String? color = "#cccccc", double? height = 28, double? fontsize = 13, EdgeInsets? margin}) {
    return Container(
      margin: margin ?? margin,
      height: height,
      child: OutlinedButton(
          style: OutlinedButton.styleFrom(
            padding: EdgeInsets.all(0.w),
            side: BorderSide(color: Tools.fromHex(color!)), // 设置边界颜色
          ),
          onPressed: onPressed,
          child: Text(
            title,
            style: TextStyle(fontSize: 13, color: Tools.fromHex(color), fontWeight: FontWeight.w500),
          )),
    );
  }
}
